<template>
  <el-container class="main_con">
    <div class="filter_con">
      <div class="button_area">
        <el-button v-has type="primary" size="medium" @click="addHandle">新增模板</el-button>
      </div>
      <div class="filter_area">
        <el-input
          class="serach_input mr_15 mt_15"
          size="medium"
          v-model="params.name"
          placeholder="关键字"
        ></el-input>
        <label class="mr_15 mt_15">状态</label>
        <el-select
          clearable
          size="medium"
          v-model="params.status"
          placeholder="请选择"
          class="mr_15 mt_15"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
        <el-button class="mr_15 mt_15" @click="searchList" size="medium">搜索</el-button>
      </div>
    </div>
    <div class="list_con">
      <el-table
        @selection-change="SelectHadnle"
        :data="list"
        element-loading-text
        fit
        highlight-current-row
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="sort" label="排序"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="carry_mode_type" label="计费方式">
          <template slot-scope="scope">
            <span>{{ scope.row.carry_mode_type == 0 ? '按件计费' : '按量计费' }}</span>
          </template>
        </el-table-column>
        <el-table-column label="首(重/件)价格">
          <template
            slot-scope="scope"
          >{{ scope.row.carry_mode_type == 0 ? scope.row.carryModeNum[0].first_amount : scope.row.carryModeWeight[0].first_amount }}</template>
        </el-table-column>
        <el-table-column label="续(重/件)价格">
          <template
            slot-scope="scope"
          >{{ scope.row.carry_mode_type == 0 ? scope.row.carryModeNum[0].second_amount : scope.row.carryModeWeight[0].second_amount }}</template>
        </el-table-column>
        <el-table-column prop="status_text" label="状态"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-has type="text" @click="EditHandle(scope.row.id)">编辑</el-button>
            <el-button
              v-has
              type="text"
              @click="EnableHandle(scope.row)"
            >{{ scope.row.status ? '禁用' : '启用' }}</el-button>
            <el-button v-has type="text" @click="handleClose(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="table_footer">
        <el-pagination
          @current-change="ChangeCurrent"
          class="pages"
          background
          layout="prev, pager, next"
          :total="total"
        ></el-pagination>
      </div>
    </div>
    <el-dialog
      :title="type == 'add' ? '新增模板' : '编辑模板'"
      destroy-on-close
      :visible.sync="drawer"
      width="1200px"
      top="60px"
      :close-on-click-modal="false"
    >
      <editfreight @on-close="beClose" :id="id" :type="type"></editfreight>
    </el-dialog>
  </el-container>
</template>

<script>
import { fitter_keyword } from "@/utils/index";
import { getfreightlist, editfreight, delfreight } from "@/api/shop/freight";
import Editfreight from "./components/Editfreight";
export default {
  data() {
    return {
      options: [
        {
          value: 1,
          label: "启用"
        },
        {
          value: 0,
          label: "禁用"
        }
      ],
      page: 1,
      params: {
        status: "",
        name: ""
      },
      listLoading: false,
      list: [],
      total: 0,
      type: "add",
      id: -1,
      drawer: false
    };
  },
  mounted() {
    this.getlist();
  },
  components: {
    Editfreight
  },
  methods: {
    searchList() {
      this.paramsdf = fitter_keyword(this.params);

      this.getlist();
    },
    getlist() {
      this.listLoading = true;
      let data = this.paramsdf;

      getfreightlist(this.page, data).then(res => {
        let jsondata = res.msg;
        this.list = jsondata.data;
        this.total = jsondata.total;
        // this.list = response.data.items
        this.listLoading = false;
      });
    },
    //启用，禁用
    EnableHandle(data) {
      let _this = this;
      let status = !data.status;
      this.$confirm(status ? "确认启用？" : "确认禁用？")
        .then(_ => {
          let params = data;
          (params.status = status),
            editfreight(params).then(res => {
              _this.getlist();
            });
        })
        .catch(_ => {});
    },
    //选择
    SelectHadnle(selection) {
      this.selectionData = selection;
    },
    //删除
    handleClose(id) {
      let _this = this;
      this.$confirm("确认删除？")
        .then(_ => {
          let data = {
            id: id
          };
          delfreight(data).then(res => {
            _this.getlist();
          });
        })
        .catch(_ => {});
    },
    //新增
    addHandle() {
      this.type = "add";
      this.id = -1;
      this.drawer = true;
    },
    //编辑
    EditHandle(id) {
      this.id = id;
      this.type = "edit";
      this.drawer = true;
    },
    gettime(value) {
      this.params.begin_date = value[0];
      this.params.end_date = value[1];
    },
    //切换页码
    ChangeCurrent(page) {
      this.page = page;
      this.getlist();
    },
    beClose() {
      this.drawer = false;
      this.getlist();
    }
  }
};
</script>

<style></style>
